package com.example.arithmeticleetcode.leetcode.nov;

import java.util.*;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2020-11-04 09:24
 **/
public class Demo01 {

    public static void main(String[] args) {
        int[] arr1 = {1, 1, 2, 2, 2, 3};
        int[] arr2 = {2, 3, 1, 3, 2};
        int[] arr3 = {-1, 1, -6, 4, 5, -6, 1, 4, 1};
        frequencySort(arr1);
        frequencySort(arr2);
        frequencySort(arr3);
        test01(5);
    }

    public static int[] frequencySort(int[] nums) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int num : nums) {
            map.put(num, map.getOrDefault(num, 0) + 1);
        }
        Map<Integer, List<Integer>> map2 = new HashMap<>();
        List<Integer> list = null;
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            if (map2.containsKey(entry.getValue())) {
                list = map2.get(entry.getValue());
                for (int i = 0; i < entry.getValue(); i++) {
                    list.add(entry.getKey());
                }
                Collections.sort(list, new Comparator<Integer>() {
                    @Override
                    public int compare(Integer o1, Integer o2) {
                        return o2 - o1;
                    }
                });
                map2.put(entry.getValue(), list);
            } else {
                list = new ArrayList<>();
                for (int i = 0; i < entry.getValue(); i++) {
                    list.add(entry.getKey());
                }
                map2.put(entry.getValue(), list);
            }
        }
        ArrayList<Integer> arrayList = new ArrayList<>(map2.keySet());
        Collections.sort(arrayList);
        int[] arr = new int[nums.length];
        int index = 0;
        for (Integer integer : arrayList) {
            for (Integer integer1 : map2.get(integer)) {
                arr[index++] = integer1;
            }
        }
        return arr;
    }

    public static void test01(int n) {
        if (n <= 3) {
            return;
        } else {
            n--;
        }

        System.out.println("111111111");
        test01( n);
        System.out.println("------------");
        if (n <= 0) {
            return;
        } else {
            n--;
        }
        System.out.println("222222222");
        test01(n);
        System.out.println("结束了");
    }
}
